<?php
namespace api\user\model;

use think\Model;
use api\user\model\UserBalanceLogModel;
use think\Db;
use api\common\model\CommonModel;
use fast\Http;
class UserModel extends CommonModel
{
    // 表名
    protected $name = 'user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $type = [
        'bankpay' => 'json',
    ];
    // 追加属性
    protected $append = [
        'mu',
        'freeze_mq',
        'team_count',
        'miner_count',
        'yday_active_mq',
        'yday_get_mq',
        'share_url'
    ];

    protected $hidden = [
        'password','salt','pay_password','pay_salt','group_id','email','level','gender','birthday','bio','money','score','successions','maxsuccessions','prevtime','logintime','loginfailure','joinip','jointime','createtime','token','wechatpay','alipay','active_status','verify_status','verify_info','loginip','verification','updatetime','avatar'
    ];

    /**
     * avatar 自动转化图片地址为绝对地址
     * @param $value
     * @return string
     */
    public function getAvatarAttr($value)
    {
        if (!empty($value)) {
            $value = cmf_get_image_url($value);
        }
        return $value;
    }

    public function getMuAttr($value,$data)
    {
        
        return $data['pay_mu']+$data['active_mu'];
    }

    /**
     * wechatpay 自动转化图片地址为绝对地址
     * @param $value
     * @return string
     */
    public function getWechatpayAttr($value)
    {
        if (!empty($value)) {
            $value = json_decode($value,true);
            $value['account_image'] = cmf_get_image_url($value['account_image']);
        }
        return $value;
    }

    /**
     * verify_info 自动转化图片地址为绝对地址
     * @param $value
     * @return string
     */
    public function getVerifyInfoAttr($value)
    {
        // if (!empty($value)) {
        //     $value = json_decode($value,true);
        //     $value['id_card_hand_image'] = cmf_get_image_url($value['id_card_hand_image']);
        //     $value['id_card_front_image'] = cmf_get_image_url($value['id_card_front_image']);
        //     $value['id_card_behind_image'] = cmf_get_image_url($value['id_card_behind_image']);
        // }

        return json_decode($value,true);
    }

    /**
     * alipay 自动转化图片地址为绝对地址
     * @param $value
     * @return string
     */
    public function getAlipayAttr($value)
    {
        if (!empty($value)) {
            $value = json_decode($value,true);
            $value['account_image'] = cmf_get_image_url($value['account_image']);
        }
        return $value;
    }

    public function getFreezeMqAttr($value,$data)
    {
        //订单类型:1=币币交易,2=OTC通道买入,3=OTC通道卖出
        //订单状态:0=已撤销,1=已完成,2=售卖中,3=待审核,4=待支付
        return Db::name('user_order')
        ->where(['user_id'=>$data['id'],'order_type'=>1,'order_status'=>2])
        ->sum('coin_num');
    }

    public function getTeamCountAttr($value,$data)
    {
        $where[] = ['exp',Db::raw("FIND_IN_SET({$data['id']},path)")];
        $where['user_id'] = ['neq',$data['id']];
        return Db::name("invite")->where($where)->count();
    }

    public function getShareUrlAttr($value,$data)
    {
        return url('index/user/register',['invite_code'=>$data['invite_code']],'',true);
    }

    /**
     * 矿机统计
     * @param $value
     * @return string
     */
    public function getMinerCountAttr($value,$data)
    {
        $where['user_id'] = $data['id'];
        return Db::name("user_miner")->where($where)->count();
    }

    public function getYdayActiveMqAttr($value,$data)
    {
        //来源:1=币币交易买入,2=币币交易卖出,3=OTC通道买入,4=OTC通道卖出,5=矿机收益,6=动态奖励,7=购买矿机,8=激活矿机,9=撤销订单,10=空投
        return Db::name('user_balance_log')
        ->where(['user_id'=>$data['id'],'log_type'=>3,'source'=>8])
        ->whereTime('createtime', 'yesterday')
        ->sum('amount');
    }

    public function getYdayGetMqAttr($value,$data)
    {
        //来源:1=币币交易买入,2=币币交易卖出,3=OTC通道买入,4=OTC通道卖出,5=矿机收益,6=动态奖励,7=购买矿机,8=激活矿机,9=撤销订单,10=空投
        return Db::name('user_balance_log')
        ->where(['user_id'=>$data['id'],'log_type'=>3,'source'=>5])
        ->whereTime('createtime', 'yesterday')
        ->sum('amount');
    }
}
